package com.zzxz.practicaltrainingsys.mapper;

import com.zzxz.practicaltrainingsys.entity.Datafile;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface DatafileMapper {

    //获取所有文件类型
    @Select("select DISTINCT data_type from datafile")
    public List<String> getDatafileTypes();

    //管理员端查询所有资料（包含项目名称和上传老师）
    @Select("SELECT p.project_name,df.data_id,df.data_name,df.data_type,df.data_url,t.teacher_name,df.data_date\n" +
            "FROM datafile df\n" +
            "JOIN project p ON df.project_id = p.project_id\n" +
            "JOIN teacher t ON df.teacher_id = t.teacher_id\n" +
            "ORDER BY df.data_date DESC")
    List<Map<String, Object>> getAllDataFiles();

    //查询文件资料列表
    @Select("select * from datafile")
    public List<Datafile> getDatafileInfo();

    //增加文件资料
    @Insert("INSERT INTO datafile (data_name,data_type, data_url, data_date,teacher_id,project_id) VALUES (#{data_name},#{data_type}, #{data_url}, now(),#{teacher_id},#{project_id})")
    int insertDatafile(Datafile datafile);

    //资料信息表删除
    @Delete("DELETE FROM datafile WHERE data_id = #{data_id}")
    int deleteDatafileById(Integer data_id);

    //实训圈存入文件信息
    @Insert("INSERT INTO datafile (data_name,data_type, data_url, data_date, _id, teacher_id) VALUES (#{data_name},#{data_type}, #{data_url}, now(), #{project_id}, #{teacher_id})")
    int circleInsertDatafile(Datafile datafile);

    @Select("SELECT df.*, t.teacher_name AS teacher_name FROM datafile df " +
            "JOIN teacher t ON df.teacher_id = t.teacher_id " +
            "WHERE df.project_id = #{project_id}")
    List<Map<String, Object>> getDataFileByStudent(@Param("project_id") Integer project_id);

    // 根据 teacher_id 和 project_id 查询文件
    @Select("SELECT p.project_name, df.data_id, df.data_name, df.data_type, df.data_url, t.teacher_name, df.data_date " +
            "FROM datafile df " +
            "JOIN project p ON df.project_id = p.project_id " +
            "JOIN teacher t ON df.teacher_id = t.teacher_id " +
            "WHERE df.teacher_id = #{teacher_id} AND df.project_id = #{project_id} " +
            "ORDER BY df.data_date DESC")
    List<Map<String, Object>> getDataFilesByTeacherAndProject(@Param("teacher_id") Integer teacher_id, @Param("project_id") Integer project_id);



    //根据project_id文件和对应id下老师姓名
    @Select("SELECT df.data_id, df.data_name, df.data_type, df.data_url, df.data_date, " +
            "df.teacher_id, df.project_id, t.teacher_name " +
            "FROM datafile df " +
            "JOIN teacher t ON df.teacher_id = t.teacher_id " +
            "WHERE df.project_id = #{project_id} " +
            "ORDER BY df.data_date DESC")
    List<Map<String, Object>> getDataFilesByProject(@Param("project_id") Integer project_id);

}
